/// @tags: CommonTrick
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

int main() {
  long long l;
  int g;
  cin >> l >> g;
  long long ans = 0;
  for (int i = 1; i <= g; ++i) {
    long long p;
    int d;
    cin >> p >> d;
    if (d == 0) {
      ans = std::max(ans, p);
    } else {
      ans = std::max(ans, l - p);
    }
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("H. Goombas Colliding.in", "r", stdin);
  freopen("H. Goombas Colliding.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
